'''
版权: Copyright (c) 2019 red

文件: services.py
创建日期: Friday January 4th 2019
作者: pao
说明:
1、分布式计算rpc服务
'''
from distribute.server import DistributeCalculatingServer


def register(jsonrpc):
    server = DistributeCalculatingServer()

    @jsonrpc.method('distribute.add_job')
    def __add_job(task_count, task_func_string, job_func_string, args):
        # 添加计算任务
        nonlocal server
        return server.add_job(task_count, task_func_string, job_func_string, args)

    @jsonrpc.method('distribute.get_job')
    def __add_job(task_count, job_index):
        # 获取计算任务
        nonlocal server
        return server.get_job(job_index)

    @jsonrpc.method('distribute.get_task')
    def __get_task(client_id):
        # 添加计算任务
        nonlocal server
        return server.get_task(client_id)

    @jsonrpc.method('distribute.return_task_error')
    def __return_task_error(job_index, task_index, error):
        # 返回计算错误
        nonlocal server
        return server.return_task_error('client_id', job_index, task_index, error)

    @jsonrpc.method('distribute.return_task_value')
    def __return_task_value(job_index, task_index, result_value):
        # 返回计算结果
        nonlocal server
        return server.return_task_value('client_id', job_index, task_index, result_value)
